﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GOFconsole
{
    public class Chapter17_Adapter
    {
        public void AdapterTest()
        {
            //模型
            Target target = new Adapter();
            target.Request();   //对客户端来说 调用的是Target的请求方法
            //DEMO
            Player a = new Forwards("巴蒂尔");
            a.Attack();
            Player b = new Guards("麦迪");
            b.Attack();
            //Player c = new Center("姚明");
            //c.Defense();        //此处的问题是 YM听不懂英文，需要一个翻译来帮他转达教练的意图

            Player ym = new Translator("姚明");
            ym.Attack();        //翻译者告诉姚明，要进攻和防守
            ym.Defense();

        }
    }

    #region 模型
    //Target类，客户所期待的接口，可以是具体的类或抽象类，也可以是接口
    class Target
    {
        public virtual void Request()
        {
            Console.WriteLine("普通请求");
        }
    }
    //Adaptee类，需要适配的类
    class Adaptee
    {
        public void SpecificRequest()
        {
            Console.WriteLine("特殊请求");
        }
    }
    //Adapter类，通过在内部包装一个Adaptee对象，把源接口转为目标接口
    class Adapter : Target
    {
        private Adaptee adaptee = new Adaptee();

        public override void Request()
        {
            adaptee.SpecificRequest();  //这样就可以把表面上调用普通请求，实际调用特殊请求
        }
    }

    #endregion

    #region Demo(姚明需要一个翻译)
    //球员抽象类
    abstract class Player
    {
        protected string name;
        public Player(string name)
        {
            this.name = name;
        }
        public abstract void Attack();
        public abstract void Defense();
    }
    //前锋、中锋、后卫
    class Forwards : Player
    {
        public Forwards(string name)
            : base(name)
        { }

        public override void Attack()
        {
            Console.WriteLine("前锋 {0} 进攻",name);
        }
        public override void Defense()
        {
            Console.WriteLine("前锋 {0 }防守",name);
        }
    }
    class Center : Player
    {
        public Center(string name)
            : base(name)
        { }

        public override void Attack()
        {
            Console.WriteLine("中锋 {0} 进攻", name);
        }
        public override void Defense()
        {
            Console.WriteLine("中锋 {0} 防守", name);
        }
    }
    class Guards : Player
    {
        public Guards(string name)
            : base(name)
        { }

        public override void Attack()
        {
            Console.WriteLine("后卫 {0} 进攻", name);
        }
        public override void Defense()
        {
            Console.WriteLine("后卫 {0} 防守", name);
        }
    }
    //构造一个外籍中锋的类来使用适配器模式
    class ForeigneCenter
    {
        private string name;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        //表明外籍球员只听得懂中文的进攻和防守方法
        public void JinGong()   
        {
            Console.WriteLine("中锋 {0} 进攻", name);
        }
        public void FangShou()
        {
            Console.WriteLine("中锋 {0} 防守", name);
        }
    }
    //翻译类(即Adapter)
    class Translator : Player
    {
        private ForeigneCenter wjzf = new ForeigneCenter();//声明并内部实例化一个外籍中锋类，表明翻译者与外籍球员有关系

        public Translator(string name)
            :base(name)
        {
            wjzf.Name = name;
        }

        public override void Attack()//由翻译者告诉外籍球员进攻是Attack
        {
            wjzf.JinGong();
        }
        public override void Defense()//由翻译者告诉外籍球员防守是Defense
        {
            wjzf.FangShou();
        }
    }
    #endregion

}
